import axios from 'axios'
import store from './../store/index.js'

axios.interceptors.request.use(function (config) {
  store.dispatch('loadingStatus', true)
  return config
}, function (error) {
  store.dispatch('loadingStatus', false)
  return Promise.reject(error)
})

export function fetch (url, params, config) {
  return new Promise((resolve, reject) => {
    axios.post(url, params, config).then(response => {
      if (response.data.respCode === '888888') {
        window.location.href = '//ac.ppdai.com/activitypage?redirect=&activityId=2067'
        return
      }
      store.dispatch('loadingStatus', false)
      resolve(response.data)
    }).catch((error) => {
      store.dispatch('loadingStatus', false)
      reject(error)
    })
  })
}

export default {
  send (url, params, config) {
    return fetch(url, params, config)
  }
}
